(function(app){
	app
		.controller("content_19_inputCtrl",["$scope","$state","myFactory","$http","$stateParams","$state","$q","$ionicHistory",function($scope,$state,myFactory,$http,$stateParams,$state,$q,$ionicHistory){
			//参数定义、接收
			var itemId = $stateParams.changeTypeId;//变更项目ID
			var chdrnum01 =  $stateParams.chdrnum01; //保单号
			var chdrcoy = $stateParams.chdrcoy; //保单公司号码
			var policyData;//保单详情
			var itemData;//用于接收初始化查询initContentItem的内容，里面存在各种各样的内容，比如item02_1
			var initFlag = false;//检测标志，在初始化函数完成后，会被置为true
			//绑定参数到页面
			$scope.chdrnum01 = chdrnum01;
			$scope.readFlag = false;//阅读协议标志，默认为false
			var token=sessionStorage.getItem("token");
			//重新受理
			$scope.renew = function(){
				myFactory.renew();
			}	
			//退出移动保全
			$scope.exit = function(){
				myFactory.exit();
			}
			//返回上一页
			$scope.goBack = function(){
				myFactory.goBack();
			}
			//客户须知弹框
			$scope.tipOpen = function(){
				modal('open','tip_model','overwindowOn19Input');
			}
			$scope.tipClose = function(){
				modal('close','tip_model','overwindowOn19Input');
			}
			//页面同意阅读协议复选框部分
			$scope.changeReadFlag = function(){
				$scope.readFlag = !$scope.readFlag;
			}
			//保单详情弹框
			$scope.showPolicyDetail = function(){
				modal('open','policyModalOn19Input','overwindowOn19Input','policyCloseXOn19Input');
				var timestamp=myFactory.getTimetamp();
				var sfis= "chdrnum-chdrcoy-token-timestamp";
				var signStr="chdrnum="+chdrnum01+"&chdrcoy="+chdrcoy+"&token="+token+"&timestamp="+timestamp;//encodeURI(name)
				var sign = myFactory.hex(signStr);
				//初始化时候查询保单详情
				$http({
					method: 'GET',
					url: app.domain + 'query/restFindPolicyDetail.action',
					params: {'chdrnum': chdrnum01,'chdrcoy': chdrcoy,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
				}).then(function successCallback(response) {
					if (response.data.success) {
						policyData = response.data.data;
						$scope.policyData = policyData;
						$scope.policyData.ptdate01=myFactory.DateTools($scope.policyData.ptdate01);
					}
				}, function errorCallback(response) {
					$.alert({
						 title: '',
						 text: '初始化查询订单详情失败!',
						 onOK: function () {
						 //点击确认
						 }
					});
				});
			}
			//保单详情弹窗关闭函数
			$scope.policyCancelOn19Input = function(){
				modal('close','policyModalOn19Input','overwindowOn19Input','policyCloseXOn19Input');
			}
			
/**********************************************初始化函数begin****************************************************/		
			//初始化查询变更项内容
			$scope.load = function () {
				myFactory.loadingShow();//显示加载圈
				var timestamp=myFactory.getTimetamp();
				var sfis= "itemId-token-timestamp";
				var signStr="itemId="+itemId+"&token="+token+"&timestamp="+timestamp;//encodeURI(name)
				var sign = myFactory.hex(signStr);
				$http({
					method: 'GET',
					url: app.domain + 'input/initContentItem.action',
					params: {'itemId': itemId,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
				}).then(function successCallback(response) {
					if (response.data.success) {
						itemData = response.data.data.modelMap;
						$scope.mcAccount = itemData.mcAccount;
						$scope.mrAccount = itemData.mrAccount;
						
						for(var i=0;i<$scope.mcAccount.length;i++){
							$scope.mcAccount[i].funddesc=$scope.mcAccount[i].funddesc.replace("投资账户","");
							//console.log($scope.mcAccount[i].funddesc);
						}
						//中信保诚「智尚人生」年金保险E款（投资连结型）add by csf 20190815 start
						var tempAccounts = [];//缓存筛选后的账户
						//中信保诚「智尚人生」年金保险E款（投资连结型）add by csf 20190815 end
						for(var i=0;i<$scope.mrAccount.length;i++){
							$scope.mrAccount[i].funddesc=$scope.mrAccount[i].funddesc.replace("投资账户","");
							//console.log($scope.mrAccount[i].funddesc);
							//中信保诚「智尚人生」年金保险E款（投资连结型）add by csf 20190815 start
							if (itemData.crtable == 'USBC') {
								if ($scope.mrAccount[i].vfund.trim() == 'IIGD'||$scope.mrAccount[i].vfund.trim() == 'MIGD') {
									tempAccounts.push($scope.mrAccount[i]);
								}
							} else {
								if ($scope.mrAccount[i].vfund.trim() != 'IIGD') {
									tempAccounts.push($scope.mrAccount[i]);
								}
							}
							//中信保诚「智尚人生」年金保险E款（投资连结型）add by csf 20190815 end
						}
						//中信保诚「智尚人生」年金保险E款（投资连结型）add by csf 20190815 start
						if (tempAccounts.length != 0 ) {
							$scope.mrAccount = tempAccounts;
						}
						//中信保诚「智尚人生」年金保险E款（投资连结型）add by csf 20190815 end
						initFlag = true;
					}
					myFactory.loadingHide();
				}, function errorCallback(response) {
					myFactory.loadingHide();
					myFactory.alert("","初始化查询变更项内容失败!","info");
				});
			}
/**********************************************初始化函数end****************************************************/			
			
			$scope.nextStep = function(){
				//判断是否初始化完成了，如果失败，不予进入下一步
				if(!initFlag){
					myFactory.alert("","初始化时失败，请返回上一步重试","info");
					return;
				}
				//获取页面数据，进行校验
				var a = $("input[name='SaleAccountRate']");//得到页面上所有名字为SaleAccountRate的元素
		    	var flag = false; 
		    	
		    	for(var i=0;i<a.length;i++){
		    		var elements=a[i].value;
		    		if(elements.trim()!=""){
		    			flag = true ;
		    			break;
		    		}
		    	}
		    	//判断是否全为空，全为空的话不允许通过
		    	if(flag == false ){
		    		$.alert({
						 title: '',
						 text: '必须填写一项转出比例',
						 onOK: function () {
						 //点击确认
						 }
					});
		    		return;
		    	}
		    	/*S2022032221697_中信保诚智尚人生年金保险C款新增“盛世嘉享、盛世优享”投资账户 add by huanghaokun 20220510 start*/
	        	//当次卖出/转出投资账户超10个
	        	var accountOutCount = 0;
	        	/*S2022032221697_中信保诚智尚人生年金保险C款新增“盛世嘉享、盛世优享”投资账户 add by huanghaokun 20220510 end*/
		    	//判断是否超过100
		    	for(var i=0;i<a.length;i++){
		    		var elements=a[i].value;
		    		if(elements.trim()>100){
		    			$.alert({
							 title: '',
							 text: '转出值每一项必须小于或等于100',
							 onOK: function () {
							 //点击确认
							 }
						});
		    			return;
		    		}
		    		/*S2022032221697_中信保诚智尚人生年金保险C款新增“盛世嘉享、盛世优享”投资账户 add by huanghaokun 20220510 start*/
		    		if("" != elements.trim()){
		    			accountOutCount++;
		    		}
			 		/*S2022032221697_中信保诚智尚人生年金保险C款新增“盛世嘉享、盛世优享”投资账户 add by huanghaokun 20220510 end*/
		    	}
		    	
		    	/*S2022032221697_中信保诚智尚人生年金保险C款新增“盛世嘉享、盛世优享”投资账户 add by huanghaokun 20220510 start*/
				if(accountOutCount>10){
					myFactory.alert("","当次卖出/转出投资账户不可超10个","info");
					myFactory.loadingHide();
					return false;
				}
				/*S2022032221697_中信保诚智尚人生年金保险C款新增“盛世嘉享、盛世优享”投资账户 add by huanghaokun 20220510 end*/
		    	
		    	/*S2022032221697_中信保诚智尚人生年金保险C款新增“盛世嘉享、盛世优享”投资账户 add by huanghaokun 20220510 start*/
	        	//当次买入/转入投资账户不可超10个
	        	var accountRateCount = 0;
	        	/*S2022032221697_中信保诚智尚人生年金保险C款新增“盛世嘉享、盛世优享”投资账户 add by huanghaokun 20220510 end*/
		        if($("BuyAccountRate_0").val()==""||$("BuyAccountRate_0").val()!=""){//即不管第一项是不是为空都进入判断
		        	var account=0;
		        	var b = $("input[name='BuyAccountRate']");  //得到页面上所有名字为BuyAccountRate的元素
					for(var i=0;i<b.length;i++){
						var v=$("#BuyAccountRate_"+i).val();  //得到那个元素上的值
						if(v.trim()>100){
			    			$.alert({
								 title: '',
								 text: '转入值每一项必须小于或等于100',
								 onOK: function () {
								 //点击确认
								 }
							});
			    			return;
			    		}
					    account=account+v*1;                  //将所有输入相加
					    /*S2022032221697_中信保诚智尚人生年金保险C款新增“盛世嘉享、盛世优享”投资账户 add by huanghaokun 20220510 start*/
					    if("" != v.trim()){
					    	accountRateCount++;
					    }
					    /*S2022032221697_中信保诚智尚人生年金保险C款新增“盛世嘉享、盛世优享”投资账户 add by huanghaokun 20220510 end*/
					}
					
					/*S2022032221697_中信保诚智尚人生年金保险C款新增“盛世嘉享、盛世优享”投资账户 add by huanghaokun 20220510 start*/
					if(accountRateCount>10){
						myFactory.alert("","当次买入/转入投资账户不可超10个","info");
						myFactory.loadingHide();
						return false;
					}
				    /*S2022032221697_中信保诚智尚人生年金保险C款新增“盛世嘉享、盛世优享”投资账户 add by huanghaokun 20220510 end*/
					
					if(account==0){
						$.alert({
							 title: '',
							 text: '转入投资账户比例为0，需重新确认!',
							 onOK: function () {
							 }
						});
						return;
					}
					if(account!=100){
						$.alert({
							 title: '',
							 text: '转入投资账户比例和不等于100%，需重新确认!',
							 onOK: function () {
							 }
						});
						return;
					}			
		        }
		        
		        //检测查阅弹框
		        var readImageFlag = $("#readImageFlag").val();
	    		if(readImageFlag=="false"){
	    			$.alert({
						 title: '',
						 text: '请阅读《申请须知》！',
						 onOK: function () {
						 //点击确认
						 }
					});
	    			return false;
	    		}
	    		//关闭遮罩
				$scope.overwindowClose = function(){
					//$(".overwindow").show();
					$(".overwindow").hide();
					$(".overmodal").hide();
				}
		        
		        
		        //校验完毕，开始封装数据
		        //买入部分
		        var a = $("input[name='BuyAccountType']");//买入类型
		        var b = $("input[name='BuyAccountRate']");//买入百分比
		        var c = $("input[name='BuyAccountName']");//账户中文
		        var d = $("input[name='SaleAccountType']");//卖出类型
		        var e = $("input[name='SaleAccountRate']");//卖出百分比
		        var f = $("input[name='SaleAccountName']");//账户中文
		        var buyType = "";
		        var buyRate = "";
		        var buyName = "";
		        var saleType = "";
		        var saleRate = "";
		        var saleName = "";
		        
		        //买入部分
		        for(var i=0;i<a.length;i++){
		        	if(i!=0){
		        		buyType = buyType + ",";
		        	}
		        	buyType = buyType + $("#BuyAccountType_"+i).val().trim();
		        }
		        
		        for(var i=0;i<b.length;i++){
		        	if(i!=0){
		        		buyRate = buyRate + ",";
		        	}
		        	buyRate = buyRate + $("#BuyAccountRate_"+i).val().trim();
		        }
		        
		        for(var i=0;i<c.length;i++){
		        	if(i!=0){
		        		buyName = buyName + ",";
		        	}
		        	buyName = buyName + $("#BuyAccountName_"+i).val().trim();
		        }
//		        console.log(buyType);
//		        console.log(buyRate);
//		        console.log(buyName);
		        
		        //卖出部分
		        for(var i=0;i<d.length;i++){
		        	if(i!=0){
		        		saleType = saleType + ",";
		        	}
		        	saleType = saleType + $("#SaleAccountType_"+i).val().trim();
		        }
		        
		        for(var i=0;i<e.length;i++){
		        	if(i!=0){
		        		saleRate = saleRate + ",";
		        	}
		        	saleRate = saleRate + $("#SaleAccountRate_"+i).val().trim();
		        }
		        
		        for(var i=0;i<f.length;i++){
		        	if(i!=0){
		        		saleName = saleName + ",";
		        	}
		        	saleName = saleName + $("#SaleAccountName_"+i).val().trim();
		        }
		        console.log(saleType);
		        console.log(saleRate);
		        console.log(saleName);
		        
		        var item={
		        	"buyType":buyType,
		        	"buyRate":buyRate,
		        	"buyName":buyName,
		        	"saleType":saleType,
		        	"saleRate":saleRate,
		        	"saleName":saleName
		        }
		        
    			/*
    			 * ********************************************************************
    			 * 	《S2018082805259-投资风险测评保全系统需求》（add by jiangwy 20190531 ) star
    			 * 
    			 * 1、检查风险评测参数信息
    			 * 2、是否进入评测页面判断
    			 * 
    			 *********************************************************************/  
		        /*$ionicHistory.clearCache().then(function(){ 
				$state.go('content_check_main',{
					itemId:itemId,
					item:item,
					chdrnum01:chdrnum01,
					chdrcoy:chdrcoy
				});
				})*/
		        
                var timestamp=myFactory.getTimetamp();
				var sfis= "token-timestamp";
				var signStr="token=" + token + "&timestamp=" + timestamp;//encodeURI(name)
				var sign = myFactory.hex(signStr);
				var reqData = {
						"chdrcoy":chdrcoy,
						"chdrnum":chdrnum01,
						"itemId": itemId,
						"itemStr":item
				}
    			$http({
    				method: 'POST',
    				url: app.domain + 'riskevaluate/checkRisk.action',
    				data:{paramsMap:reqData,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
    			}).then(function successCallback(response) {
    				if (response.data.success) {
    					var resultData = response.data.data.modelMap;  
    					//	【本次买入账户风险等级】
    					$scope.inputRiskGrade = resultData.inputRiskGrade;
    					//	是否需要风险评测
    					$scope.isEvaluate     = resultData.isEvaluate;
    					//	路由 nextUrl 地址，默认为：审核页面
    					var nextUrl = "content_check_main";
    					//	如果风险评测结果值为“N”，则直接跳过该风险评测页面进入审核页面
    					if( "Y" == $scope.isEvaluate ){
    						//	进入风险评测页面
    						nextUrl = "invest_risk_evaluate";
    					}
    					
    					//	路由跳转
//		                $ionicHistory.clearCache().then(function(){
		                    $state.go( nextUrl ,{
		                        itemId:itemId,
		                        item:item,
		                        chdrnum01:chdrnum01,
		                        chdrcoy:chdrcoy,
		                        inputRiskGrade:$scope.inputRiskGrade
		                    });
//		                })
    				}
    				myFactory.loadingHide();
    			}, function errorCallback(response) {
    				myFactory.loadingHide();
    				myFactory.alert("","初始化查询风险评测失败!","info");
    			});
    			
   			 	/* 
    			 * ********************************************************************
    			 * 	《S2018082805259-投资风险测评保全系统需求》（add by jiangwy 20190531 ) end
    			 * 
    			 * ********************************************************************/ 
    			
		        
			}
			
			/**
			 * 只输入整数函授
			 */
			$scope.keyNumPress = function(){
				var keyCode = event.keyCode;
				if(keyCode>=48 && keyCode<=57){
					event.returnValue = true;
				}else{
					event.returnValue = false;
				}
			}
			
			
			
		}]);
	
		//指针弹窗
		app.directive('infoid',function(){
			return {
				restrict: 'EA',		//指明指令在DOM里面以什么形式被声明,EA.表示即可以是元素也可以是属性
				templateUrl: 'html/input/content_base_info.html'+app.clearCache,
				replace: true,		//设置解析后，DOM声明的指令是否消失
				link: function(scope,element,attrs){  //链接函数负责注册DOM事件和更新DOM,
					console.log('指令完成');
				}
			}
		});
})(app)